"""
Base Builder Interface

Defines the abstract interface for building solver frameworks.
"""

from abc import ABC, abstractmethod
from typing import Dict, List, Optional, Any
from pathlib import Path


class BuildResult:
    """Result of a build operation."""
    
    def __init__(self, success: bool, message: str = "", artifacts: Optional[List[str]] = None):
        self.success = success
        self.message = message
        self.artifacts = artifacts or []
        
    def __bool__(self) -> bool:
        return self.success


class BaseBuilder(ABC):
    """
    Abstract base class for framework builders.
    
    Builders handle compilation and build management for different solver frameworks.
    """
    
    def __init__(self, extension_name: str):
        """
        Initialize the builder.
        
        Args:
            extension_name: Name of the extension this builder belongs to
        """
        self.extension_name = extension_name
        
    @abstractmethod
    def can_build(self, source_path: Path) -> bool:
        """
        Check if this builder can build the source at the given path.
        
        Args:
            source_path: Path to source directory
            
        Returns:
            True if builder can handle this source
        """
        pass
    
    @abstractmethod
    def build(self, source_path: Path, build_config: Optional[Dict[str, Any]] = None, 
              clean: bool = False, verbose: bool = False) -> BuildResult:
        """
        Build the solver framework.
        
        Args:
            source_path: Path to source directory
            build_config: Build configuration parameters
            clean: Whether to perform a clean build
            verbose: Whether to enable verbose output
            
        Returns:
            BuildResult indicating success/failure and build artifacts
        """
        pass
    
    @abstractmethod
    def get_build_artifacts(self, source_path: Path) -> List[str]:
        """
        Get list of build artifacts (executables, libraries, etc.).
        
        Args:
            source_path: Path to source directory
            
        Returns:
            List of artifact paths relative to source_path
        """
        pass
    
    def validate_dependencies(self) -> List[str]:
        """
        Validate that build dependencies are available.
        
        Returns:
            List of error messages (empty if valid)
        """
        return []
    
    def get_build_info(self, source_path: Path) -> Dict[str, Any]:
        """
        Get information about the build system.
        
        Args:
            source_path: Path to source directory
            
        Returns:
            Dictionary with build system information
        """
        return {
            'builder': self.__class__.__name__,
            'extension': self.extension_name,
            'source_path': str(source_path),
        }
    
    def clean(self, source_path: Path) -> BuildResult:
        """
        Clean build artifacts.
        
        Args:
            source_path: Path to source directory
            
        Returns:
            BuildResult indicating success/failure
        """
        return BuildResult(success=True, message="Clean not implemented")
    
    def get_default_config(self) -> Dict[str, Any]:
        """
        Get default build configuration.
        
        Returns:
            Default configuration dictionary
        """
        return {}
    
    def validate_config(self, config: Dict[str, Any]) -> List[str]:
        """
        Validate build configuration.
        
        Args:
            config: Build configuration to validate
            
        Returns:
            List of validation error messages (empty if valid)
        """
        return []
    
    def estimate_build_time(self, source_path: Path) -> Optional[int]:
        """
        Estimate build time in seconds.
        
        Args:
            source_path: Path to source directory
            
        Returns:
            Estimated build time in seconds, or None if unknown
        """
        return None
    
    def __str__(self) -> str:
        """String representation of the builder."""
        return f"{self.__class__.__name__}({self.extension_name})"
        
    def __repr__(self) -> str:
        """Detailed string representation."""
        return f"<{self.__class__.__name__}(extension='{self.extension_name}')>"